package bdz.projects.atwww.atlas.model
{
	import bdz.common.events.DispatchChannel;
	import bdz.common.events.GenericEvent;
	
	

	public dynamic class BHPModel extends Array
	{
		private static var instance:BHPModel;
		private var filtered:Array;
		public function BHPModel(se:SingletonEnforcer)
		{
			super();
		}
		public static function getModel(def:XMLList = null):BHPModel{
			if(!instance){
				if(def == null){
					throw new Error("Error: Default XMLList value needed on initial instantiation");
					return;
				}
				instance = new BHPModel(new SingletonEnforcer());
				instance.initialize(def);
			}
			return instance;
			
		}
		private function initialize(def:XMLList):void{
			filtered = new Array();
			var counter:Number = 0;
			for each(var xm:XML in def){
				var bitem:BHPItem = new BHPItem(xm);
				bitem.index = counter;
				instance.push(bitem);
				counter++;
			}
			DispatchChannel.getChannel().dispatchEvent(new GenericEvent(GenericEvent.MATCHED, this));
		}
		public function clearFilter():void{
			filtered = [];
			DispatchChannel.getChannel().dispatchEvent(new GenericEvent(GenericEvent.MATCHED, this));
		}
		public function singleFilter(o:Object):void{
			filtered = [];
			for each(var bitem:BHPItem in this){
				if(bitem[o.prop] == o.value){
					filtered.push(bitem);
				}
			}
			DispatchChannel.getChannel().dispatchEvent(new GenericEvent(GenericEvent.MATCHED, filtered));
		}
		
		public function doubleFilter(o1:Object, o2:Object):void{
			filtered = [];
			for each(var bitem:BHPItem in this){
				if(bitem[o1.prop] == o1.value && bitem[o2.prop] == o2.value){
					filtered.push(bitem);
				}
			}
			DispatchChannel.getChannel().dispatchEvent(new GenericEvent(GenericEvent.MATCHED, filtered));
		}
		public function getItemAt(index:Number):BHPItem{
			return this[index];
		}
		public function get dataSet():Array{
			return filtered;
		}
		
		
	}
}
/**
 * @private
 * 
 */
internal class SingletonEnforcer{}